home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 235 / Issue 235 - September 2007 - DPCS0907DVD.ISO / Extras / NetObjects Fusion / NOF10.exe / data1.cab / FSI / lib / nof / util / Iterator.js < prev    next >
Encoding:
Text File  |  2007-04-11  |  2.3 KB  |  94 lines

  1. /****i* SOURCE_FILE/INFO
  2.     *
  3.     * NAME
  4.     *  Iterator.js
  5.     *
  6.     * USAGE
  7.     *  Part of Netobjects JavaScript Library.
  8.     *
  9.     * COPYRIGHT
  10.     *  Copyright ⌐ 2000-2005 Website Pros, Inc.
  11.     *  All Rights Reserved.
  12.     *
  13.     *  This is an unpublished work protected by Website Pros, Inc.
  14.     *  as a trade secret, and is not to be used or disclosed except as
  15.     *  expressly provided in a written license agreement executed by
  16.     *  you and Website Pros, Inc.
  17.     *
  18.     *      <copyright@websitepros.com>
  19.     *
  20.     * NOTES
  21.     *  JavaScript code.
  22.     *
  23.     *****/
  24.  
  25. if (!IS.isModuleInitialized("IS.NOF.UTIL.Iterator"))
  26. {      
  27.     /****h* NOF_JavaScript_Library/NOF.UTIL.Iterator
  28.     *
  29.     * NAME
  30.     *  NOF.UTIL.Iterator
  31.     *
  32.     * DESCRIPTION
  33.     *   An iterator over a collection.
  34.     *
  35.     ****/
  36.     function UTIL_Iterator(/*Collection*/ c, /*boolean*/ safe) {
  37.         this.__proto__    = UTIL_Iterator.prototype;
  38.  
  39. //TODO:    if safe then clone the collection and work on it's copy        
  40.         
  41.         this.collection        = (safe == true) ? c.toArray() : c; //? c.toArray();
  42.         this.currentIndex    = -1;
  43.         this.canDelete        = false;
  44.         this.isNextAvailable = this.hasNext();
  45.     }
  46.     {
  47.         var method        = UTIL_Iterator.prototype;
  48. /*
  49.  boolean hasNext()           
  50.  Object next() 
  51.  void remove()           
  52. */
  53.         /**
  54.         * Returns true if the iteration has more elements.        
  55.         **/
  56.         method.hasNext    = function () {
  57.             return (this.currentIndex + 1 < this.collection.length);
  58.         }
  59.         
  60.         /**
  61.         * Returns the next element in the iteration.         
  62.         **/
  63.         method.next    = function () {
  64.             
  65.             if (!this.isNextAvailable) {
  66.                 throw new NOF.UTIL.Exception("NoSuchElementException", "NOF.UTIL.Iterator", 67);
  67.             } else {
  68.                 this.canDelete = true;
  69.                 this.currentIndex++;
  70.                 this.isNextAvailable = this.hasNext();
  71.                 return this.collection.item(this.currentIndex);
  72.             }
  73.         }
  74.         
  75.         /**
  76.         * Removes from the underlying collection the last element
  77.          * returned by the iterator.
  78.         **/
  79.         method.remove    = function () {
  80.             if (!this.canDelete) {
  81.                 throw new NOF.UTIL.Exception("IllegalStateException", "NOF.UTIL.Iterator", 82);
  82.                 //return;
  83.             }
  84.             if (this.currentIndex < this.collection.length) {
  85.                 this.collection.remove(this.currentIndex--);
  86.                 this.canDelete = false;
  87.                 this.isNextAvailable = this.hasNext();                
  88.             }
  89.         }
  90.         
  91.     }    
  92.     
  93.     UTIL.__proto__.Iterator = UTIL_Iterator;
  94. }